package com.star.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;


import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
 * @description:
 * @author: KYB
 * @date: 2020-07-07 15:02
 */
@Component
public class JavaMailUtil  {
    @Autowired
    JavaMailSender MailSender;

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public JavaMailSender getMailSender() {
        return MailSender;
    }

    public void setMailSender(JavaMailSender mailSender) {
        MailSender = mailSender;
    }

    public TemplateEngine getTemplateEngine() {
        return templateEngine;
    }

    public void setTemplateEngine(TemplateEngine templateEngine) {
        this.templateEngine = templateEngine;
    }

    @Value("${spring.mail.username}")
    private String from;

    //发送简单消息
    public int sendSimpleMail(String to,String text,String subject){
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        simpleMailMessage.setFrom(from);
        simpleMailMessage.setTo(to);
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(text);

        try {
            System.out.println(simpleMailMessage);
            System.out.println(MailSender);
            MailSender.send(simpleMailMessage);
            return 1;//发送成功
        } catch (MailException e) {
            e.printStackTrace();
            return 0;//发送失败
        }

    }

    //发送html格式文本
    public int sendHtmlMail(String to,String subject,String text){
        MimeMessage mimeMessage=MailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(text,true);//html格式为true
            MailSender.send(mimeMessage);
            return 1;
        } catch (MessagingException e) {
            e.printStackTrace();
            return 0;
        }


    }


    public int sendAttachMail(String to,String text,String subject,String path){
        MimeMessage mimeMessage=MailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true,"utf-8");
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setText(text,true);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setTo(to);
            FileSystemResource fileSystemResource=new FileSystemResource(new File(path));
            String fileName=path.substring(path.lastIndexOf(File.separator));
            mimeMessageHelper.addAttachment(fileName,fileSystemResource);
            MailSender.send(mimeMessage);
            return 1;

        } catch (MessagingException e) {
            e.printStackTrace();
            return 0;
        }

    }



    public int sendPicMail(String to,String text,String subject,String path,String id){
        MimeMessage mimeMessage=MailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setText(text,true);
            mimeMessageHelper.setSubject(subject);
            FileSystemResource fileSystemResource=new FileSystemResource(new File(path));
            mimeMessageHelper.addInline(id,fileSystemResource);
            MailSender.send(mimeMessage);
            return 1;


        } catch (MessagingException e) {
            e.printStackTrace();
            return 0;
        }

    }
    @Autowired
    TemplateEngine templateEngine;

    public int sendTemplateMail(String to,String subject,String name,String company,String photo,int jid){
        //创建邮件正文
        Context context = new Context();
        context.setVariable("name", name);
        context.setVariable("company", company);
        context.setVariable("url", "http://www.tap2019.top:2019/front/jobpage.action?"+jid);
        context.setVariable("photo","http://www.tap2019.top:2019/"+photo);
        System.out.println(templateEngine+"模板引擎");
        String emailContent = templateEngine.process("j", context);
        int result=sendHtmlMail(to,subject,emailContent);
        return result;
    }

}
